package control.server;

	import java.io.IOException;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.ParseException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;

	public class SedCup2013Client {

		
		private static Response request;
	    private static final String URL = "http://localhost:3723/sedcup2013/teams/Client2";

	    public static void main(String[] args) throws InterruptedException {
	        // switch to the url from args if present
	        String url = URL;
	        if (args.length != 0) {
	            url = args[0];
	        }

	        HttpClient httpclient = new DefaultHttpClient(); // create the http client
	        // httpclient.getParams().setParameter("http.socket.timeout", new Integer(3600000)); // set the timeout if needed
	        System.out.println("Connecting to " + url + "...");
	        try {
	            HttpPost httppost = new HttpPost(url); // create an object for the post message
	            String emptyRequest = "{\"turn\":0,\"hire\":[0,0,0],\"move\":[],\"dojob\":[],\"fire\":[]}"; // this will be the message's content
	            System.out.println("request: " + emptyRequest);
	            StringEntity strentity = new StringEntity(emptyRequest, "UTF-8"); // pack it into a StringEntity
	            strentity.setContentType("application/json"); // set the type to json
	            httppost.setEntity(strentity); // add the content the post message

	            HttpResponse response = httpclient.execute(httppost); // post the message and wait for response
	            HttpEntity entity = response.getEntity(); // get the entity
	            final String str = EntityUtils.toString(entity, "UTF-8"); // unpack the entity
	            System.out.println("response: " + str);
	            request = new Response(str);
	            
	        } catch (ParseException e) {
	            e.printStackTrace();
	        } catch (IOException e) {
	            e.printStackTrace();
	        } finally {
	            httpclient.getConnectionManager().shutdown(); // release all allocated resources
	        }
	        System.out.println("Finished.");
	    }

	}

